package com.csust.travel.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.travel.Constants.ResultCode;
import com.csust.travel.entity.*;
import com.csust.travel.exception.ServiceException;
import com.csust.travel.mapper.ProductInfoMapper;
import com.csust.travel.model.DTO.ProductInfoDto;
import com.csust.travel.model.DTO.ProductInfoPageDto;
import com.csust.travel.model.R;
import com.csust.travel.model.VO.ProductInfoDetailVo;
import com.csust.travel.model.VO.ProductInfoPageVo;
import com.csust.travel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {


    @Autowired
    private UserService userService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductCollectService productCollectService;

    @Autowired
    private ProductVoucherService productVoucherService;


    @Override
    public R createProductInfo(ProductInfoDto dto) {
        ProductInfo productInfo = BeanUtil.toBean(dto, ProductInfo.class);
        if (StrUtil.isNotEmpty(dto.getType())) {
            ProductType productType = productTypeService.lambdaQuery().eq(ProductType::getTypeCode, dto.getType()).one();
            if (!BeanUtil.isEmpty(productType)) {
                productInfo.setTypeCode(productType.getTypeCode());
                productInfo.setTypeName(productType.getTypeName());
            }
        }
        productInfo.setPrice((long) (dto.getPrice() * 100));
        productInfo.setOriginalPrice((long) (dto.getOriginalPrice() * 100));
        productInfo.setStatus(1);
        productInfo.setLikeCount(0);
        productInfo.setUserId(StpUtil.getLoginIdAsString());
        productInfo.setCreateTime(new Date().getTime());
        productInfo.setUpdateTime(new Date().getTime());
        boolean save = save(productInfo);
        if (!save) throw new ServiceException(ResultCode.SaveError);
        return R.ok();
    }


    @Override
    public List<ProductInfoPageVo> getProductList(ProductInfoPageDto pageDto) {
        Page<ProductInfo> page = lambdaQuery()
                .eq( StrUtil.isNotEmpty(pageDto.getTypeCode()), ProductInfo::getTypeCode, pageDto.getTypeCode())
                .eq(ProductInfo::getStatus, 9)
                .and(StrUtil.isNotEmpty(pageDto.getKey()), wrapper -> {
                    wrapper.like(StrUtil.isNotEmpty(pageDto.getKey()), ProductInfo::getTitle, pageDto.getKey());
                })
                .orderByDesc(ProductInfo::getCreateTime)
                .page(new Page<ProductInfo>(pageDto.getPageNumber(), pageDto.getPageSize()));
        List<ProductInfo> records = page.getRecords();
        List<ProductInfoPageVo> voList = records.stream().map(item -> {
            User user = userService.getById(item.getUserId());
            ProductInfoPageVo vo = BeanUtil.toBean(item, ProductInfoPageVo.class);
            if (!BeanUtil.isEmpty(user)) {
                vo.setAvatar(user.getAvatar());
                vo.setNickName(user.getNickName());
            }
            return vo;
        }).collect(Collectors.toList());
        System.out.println("123456 "+voList);
        return voList;
    }


    @Override
    public List<ProductInfo> getMyProductInfoList() {
        String userId = StpUtil.getLoginIdAsString();
        List<ProductInfo> list = lambdaQuery()
                .eq(ProductInfo::getUserId, userId)
                .ne(ProductInfo::getStatus,10)
                .orderByDesc(ProductInfo::getCreateTime).list();
        return list;
    }



    @Override
    public List<ProductInfo> getMyProductCollectInfo() {
        String userId = StpUtil.getLoginIdAsString();
        List<ProductCollect> collectList = productCollectService.lambdaQuery().eq(ProductCollect::getUserId, userId).list();
        List<ProductInfo> collect1 = collectList.stream().map(collect -> {
            ProductInfo productInfo = getById(collect.getProductId());
            ProductVoucher one = productVoucherService.lambdaQuery().eq(ProductVoucher::getProductId, productInfo.getId()).one();
            if(!BeanUtil.isEmpty(one)){
                productInfo.setProductVoucher(one);
            }
            return productInfo;
        }).collect(Collectors.toList());
        System.out.println("1232"+collect1);
        return collect1;

    }

    @Override
    public ProductInfoDetailVo getProductInfo(String productId) {
        ProductInfo productInfo = getById(productId);
        if (BeanUtil.isEmpty(productInfo)) throw new ServiceException(ResultCode.NotFindError);
        ProductInfoDetailVo productInfoDetailVo = BeanUtil.toBean(productInfo, ProductInfoDetailVo.class);
        User user = userService.getById(productInfo.getUserId());
        if (BeanUtil.isEmpty(user)) throw new ServiceException(ResultCode.NotFindError);
        productInfoDetailVo.setUserInfo(user);
        return productInfoDetailVo;
    }
}
